﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

using MapInfo.Data;
using MapInfo.Mapping;
using MapInfo.Engine;
using MapInfo.Geometry;
using MapInfo.Styles;
using MapInfo.Tools;
using MapInfo.Windows.Controls;

namespace MapX
{
    public partial class MapForm1 : Form
    {
        public MapForm1()
        {
            InitializeComponent();
            mapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(@"WORLD.TAB"));
            mapControl1.Map.ViewChangedEvent += new MapInfo.Mapping.ViewChangedEventHandler(Map_ViewChangedEvent);
            Map_ViewChangedEvent(this, null);
        }

        void Map_ViewChangedEvent(object sender, MapInfo.Mapping.ViewChangedEventArgs e)
        {
            // Display the zoom level
            Double dblZoom = System.Convert.ToDouble(String.Format("{0:E2}", mapControl1.Map.Zoom.Value));
            if (statusStrip1.Items.Count > 0)
            {
                statusStrip1.Items[0].Text = "缩放: " + dblZoom.ToString() + " " + MapInfo.Geometry.CoordSys.DistanceUnitAbbreviation(mapControl1.Map.Zoom.Unit);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num = Convert.ToInt32(tb.Text.ToString());
            if(num==10)
            {
                MessageBox.Show("已经是最大了！");
                return;
            }
            else
            {
                num++;
            }
            this.tb.Text = num.ToString();
            if (num < 1 || num > 10) return;
            MapInfo.Geometry.Distance previousZoom = this.mapControl1.Map.Zoom;
            mapControl1.Map.Zoom = new MapInfo.Geometry.Distance(previousZoom.Value / (2 * 1), previousZoom.Unit);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int num = Convert.ToInt32(tb.Text.ToString());
            if(num==0)
            {
                MessageBox.Show("已经是最小了！");
                return;
            }
            else
            {
                num--;
            }
            this.tb.Text = num.ToString();
            MapInfo.Geometry.Distance previousZoom = this.mapControl1.Map.Zoom;
            mapControl1.Map.Zoom = new MapInfo.Geometry.Distance(previousZoom.Value * (2 * 1), previousZoom.Unit);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MapInfo.Geometry.Distance previousZoom = this.mapControl1.Map.Zoom;
            mapControl1.Map.Zoom = new MapInfo.Geometry.Distance(previousZoom.Value * (2 * 1), previousZoom.Unit);
        }
        private void con_access()
        {
            #region Access 2007
            Console.WriteLine("Access 2007");
            string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
            strConnection += @"Data Source=roominfo.accdb;";
            strConnection += "Persist Security Info=False";
            using (OleDbConnection objConnection = new OleDbConnection(strConnection))
            {
                objConnection.Open();
                OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from room where room_id=801", objConnection);
                DataSet ds = new DataSet();
                myCommandd.Fill(ds, "room");
                DataTable dt = ds.Tables["room"];
                
                textBox1.Text = dt.Columns[0].ToString() + "\r\n" + dt.Columns[1].ToString() + "\r\n" + dt.Columns[2].ToString() + "\r\n" + dt.Columns[3].ToString();
                objConnection.Close();
            }
            #endregion
        }

        private void btn_db_Click(object sender, EventArgs e)
        {
            #region Access 2007
            string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
            strConnection += @"Data Source=roominfo.accdb;";
            strConnection += "Persist Security Info=False";
            using (OleDbConnection objConnection = new OleDbConnection(strConnection))
            {
                objConnection.Open();
                OleDbCommand cmd = objConnection.CreateCommand();
                cmd.CommandText = "select * from room where room_id=801";
                OleDbDataReader reader = cmd.ExecuteReader();
                while(reader.Read())
                {
                    room_num.Text = reader["room_id"] + "\r\n";
                    mem_info.Text = reader["members"] + "\r\n";
                    temperature.Text = reader["temperature"] + "\r\n";
                    lord.Text = reader["lord"] + "\r\n";
                    textBox1.Text = reader["room_id"] + "\r\n" + reader["members"] + "\r\n" + reader["temperature"] + "\r\n" + reader["lord"] + "\r\n";
                }
                objConnection.Close();
            }
            #endregion
            textBox1.Text += "XML message:\r\n";
            Tools tl = new Tools();
            tl.set_config("config.xml");
            textBox1.Text += "Port:" + tl.port + "\r\n";
            textBox1.Text += "BaudRate:" + tl.BaudRate + "\r\n";
        }

        private void mapControl1_MouseClick(object sender, MouseEventArgs e)
        {
            //MapInfo.Data.Catalog _catalog = MapInfo.Engine.Session.Current.Catalog;
            System.Drawing.PointF DisplayPoint = new PointF(e.X, e.Y);
            MapInfo.Geometry.DPoint MapPoint = new MapInfo.Geometry.DPoint();

            MapInfo.Geometry.DisplayTransform converter = this.mapControl1.Map.DisplayTransform;
            converter.FromDisplay(DisplayPoint, out MapPoint);
            try
            {
                MapInfo.Mapping.Map _map = mapControl1.Map;
                MapInfo.Data.Catalog _catalog = MapInfo.Engine.Session.Current.Catalog;
                Cursor.Current = Cursors.WaitCursor;

                //设置查找半径
                MapInfo.Geometry.Distance d = MapInfo.Mapping.SearchInfoFactory.ScreenToMapDistance(_map, 3);
                MapInfo.Data.SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchNearest(MapPoint, _map.GetDisplayCoordSys(), d);
                MapInfo.Data.IResultSetFeatureCollection irfc = _catalog.Search("WORLD", si);//WORLD是地图名，可以改成变量

                MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
                MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(irfc);

                this.textBox1.Clear();
                foreach (MapInfo.Data.Feature f in irfc)
                {
                    if (f.ToString() != "")
                    {
                        textBox1.Text = "key:\t\t" + f.Key.ToString() + "\r\t" + "BaseTable:\t" + irfc.BaseTable.Alias + "\r\t" + "Table:\t\t" + f.Table + "\r\t" + "Feature:\t" + f.ToString() + "\r\t";
                    }
                }
                irfc.Clear();
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void mapControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void mapControl1_MouseMove(object sender, MouseEventArgs e)
        {
            System.Drawing.PointF DisplayPoint = new PointF(e.X, e.Y);
            MapInfo.Geometry.DPoint MapPoint = new MapInfo.Geometry.DPoint();
            MapInfo.Geometry.DisplayTransform converter = this.mapControl1.Map.DisplayTransform;
            converter.FromDisplay(DisplayPoint, out MapPoint);
            toolStripStatusLabel1.Text = "DisplayPoint: " + e.X.ToString() + ", " + e.Y.ToString() + " Cursor Location: " + MapPoint.x.ToString() + ", " + MapPoint.y.ToString();
        }
        private void write_port(String szWrite)//预留写串口接口
        {
            serialPort1.PortName = "COM1";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
            //            byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
            byte[] data = Encoding.Unicode.GetBytes(szWrite);
            string str = Convert.ToBase64String(data);
            serialPort1.WriteLine(str);
            MessageBox.Show("数据发送成功！", "系统提示");
        }
        private String read_port()//预留读串口接口
        {
            byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
            //           textBox2.Text = Encoding.Unicode.GetString(data);
            serialPort1.Close();
            MessageBox.Show("数据接收成功！", "系统提示");
            return Encoding.Unicode.GetString(data);
        }
    }
}